package com.fyzk.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtils {

    /**
     * 判断机子当前是否能够连接网络
     *
     * @param context 上下文环境
     * @return boolean 网络是否连接(数据流量或是wifi)
     */
    public static boolean isNetworkConnected(Context context) {
        if (context == null) {
            return false;
        }
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobNetInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNetInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return ((mobNetInfo != null && mobNetInfo.isConnectedOrConnecting())
                || (wifiNetInfo != null && wifiNetInfo.isConnectedOrConnecting()));
    }

    /**
     * 判断机子当前是否能处于wifi环境
     *
     * @param context 上下文环境
     * @return boolean 网络是否处于wifi环境
     */
    public static boolean isNetworkUnderWifi(Context context) {
        if (context == null) {
            return true;
        }
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return (wifiNetInfo != null && wifiNetInfo.isConnectedOrConnecting());
    }

    /**
     * 判断网络处于有网状态，并且非wife
     *
     * @param context
     * @return true 是4G状态
     */
    public static boolean isNetWorkConnectedAlsoUnWifi(Context context) {
        return isNetworkConnected(context) && !isNetworkUnderWifi(context);
    }

    /**
     * 判断机子当前是否能处于wifi环境
     *
     * @param context 上下文环境
     * @return boolean 网络是否处于wifi环境
     */
    public static int isNetworkUnderWifiInt(Context context) {
        if (context == null) {
            return 2;
        }
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isWifi = wifiNetInfo != null && wifiNetInfo.isConnectedOrConnecting();
        if (isWifi) {
            return 1;
        } else {
            return 2;
        }
    }
}
